//
//  MCConsumeViewController.m
//  ERP
//
//  Created by QA on 15/11/9.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MCConsumeViewController.h"
#import "YFViewPager.h"
#import "GEDateFromatter.h"

#import "MCConsumeViewItemOneController.h"
#import "MCConsumeViewItemTwoController.h"
#import "MCSearchChargeInViewController.h"

@interface MCConsumeViewController ()

@property (nonatomic, strong) YFViewPager *viewPager;

@property (nonatomic, strong) MCConsumeViewItemOneController *view1;
@property (nonatomic, strong) MCConsumeViewItemTwoController *view2;

@property (strong, nonatomic) NSString *searchStartTime;
@property (strong, nonatomic) NSString *searchEndTime;
@property (nonatomic,strong) UILabel *searchTimeLabel;

@end

@implementation MCConsumeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"原材消耗统计分析"];
        
        //设置搜索按钮
        UIButton* righButtont= [UIButton buttonWithType:UIButtonTypeCustom];
        righButtont.frame = CGRectMake(0, 0, 44, 44);
        [righButtont setImage:[UIImage imageNamed:@"home_nav_left_serch"] forState:UIControlStateNormal];
        [righButtont addTarget:self action:@selector(searchChargeInSelector:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:righButtont];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
}

#pragma mark - 配置视图
- (void)configUI
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 16)];
    bgView.backgroundColor = ThemeColorBlue;
    [self.view addSubview:bgView];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 12)];
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:timeLabel];
    self.searchTimeLabel = timeLabel;
    
    //设置默认搜索时间(本月)
    NSString *time = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM"];
    self.searchStartTime = [NSString stringWithFormat:@"%@-01",time];
    self.searchEndTime = [GEDateFromatter formmatTimeDate:[NSDate date]];
    
    NSString *startTime = [self.searchStartTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    NSString *endTime = [self.searchEndTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    self.searchTimeLabel.text = [NSString stringWithFormat:@"检索时间 %@ - %@",startTime, endTime];
    
    //设置滑动页面
    self.view1 = [[MCConsumeViewItemOneController alloc] init];
    self.view2 = [[MCConsumeViewItemTwoController alloc] initWithNavigationController:self.navigationController];
    
    NSArray *titles = [[NSArray alloc] initWithObjects: @"材料消耗", @"工程消耗", nil];
    NSArray *icons = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"issueTree"], [UIImage imageNamed:@"hollow_out_star_blue"], nil];
    
    NSArray *views = [[NSArray alloc] initWithObjects: _view1.view, _view2.view, nil];
    _viewPager = [[YFViewPager alloc] initWithFrame:CGRectMake(0, 16, SuperWidth, SuperHeight - 64)
                                             titles:titles icons:icons selectedIcons:nil views:views];
    _viewPager.tabSelectedArrowBgColor = ThemeColorBlue;
    _viewPager.tabSelectedTitleColor = ThemeColorBlue;
    _viewPager.tabTitleColor = [UIColor colorWithWhite:0 alpha:0.75];
    
//    _viewPager.tabBgColor = ThemeColorBlue;
//    _viewPager.tabSelectedArrowBgColor = ThemeColorBlue;
//    _viewPager.tabSelectedTitleColor = ThemeColorBlue;
//    _viewPager.tabSelectedArrowBgColor = ThemeColorBlue;
//    _viewPager.tabArrowBgColor = ThemeColorBlue;
//    _viewPager.tabTitleColor = [UIColor whiteColor];
    [self.view addSubview:_viewPager];
    
#pragma mark - YFViewPager 相关属性 方法
//    _viewPager.enabledScroll = NO;
//    _viewPager.showAnimation = NO;
//    _viewPager.showBottomLine = NO;
//    _viewPager.showVLine = NO;
//    _viewPager.tabSelectedArrowBgColor = [UIColor redColor];
//    _viewPager.tabArrowBgColor = [UIColor orangeColor];
//    _viewPager.tabBgColor = [UIColor orangeColor];
//    _viewPager.tabSelectedArrowBgColor = ThemeColorBlue;
//    _viewPager.tabSelectedBgColor = [UIColor redColor];
//    _viewPager.tabSelectedTitleColor = ThemeColorBlue;
//    _viewPager.tabTitleColor = [UIColor colorWithWhite:0 alpha:0.75];
//    [_viewPager setTitleIconsArray:icons selectedIconsArray:nil];
    
    // 此方法用于给菜单标题右上角小红点赋值，可动态调用
    //[_viewPager setTipsCountArray:@[@100,@8,@0]];
    
    // 点击菜单时触发
//    [_viewPager didSelectedBlock:^(id viewPager, NSInteger index) {
//        
//    }];
}

#pragma mark -
#pragma mark IBAction

- (void)searchChargeInSelector:(id)sender
{
    MCSearchChargeInViewController *view = [[MCSearchChargeInViewController alloc] init];
    view.backResultDelagate = self;
    AbstractNavigationController *navigation = [[AbstractNavigationController alloc] initWithRootViewController:view];
    [self.navigationController presentViewController:navigation animated:YES completion:nil];
}

#pragma mark - Other Method

- (void)backResultDataKey:(id)key dataValue:(id)backValue
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    if (!backValue) return;
    
    NSArray *array = backValue;
    self.searchStartTime = array[0];
    self.searchEndTime = array[1];
    
    NSString *startTime = [self.searchStartTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    NSString *endTime = [self.searchEndTime stringByReplacingOccurrencesOfString:@"-" withString:@"."];
    self.searchTimeLabel.text = [NSString stringWithFormat:@"检索时间 %@ - %@",startTime, endTime];
    
    [self.view1 updateTableStartTime:self.searchStartTime endTime:self.searchEndTime];
    [self.view2 updateTableStartTime:self.searchStartTime endTime:self.searchEndTime];
}

@end
